﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;

public partial class AboutusEditEN : System.Web.UI.Page
{

    public List<AboutUS> lstAboutUSGrid
    {
        get
        {
            if (ViewState["lstAboutUSGrid"] == null)
            {
                ViewState["lstAboutUSGrid"] = System.Guid.NewGuid().ToString();
            }


            if (Session[ViewState["lstAboutUSGrid"].ToString()] == null)
            {
                Session[ViewState["lstAboutUSGrid"].ToString()] = new List<AboutUS>();

            }

            return (List<AboutUS>)Session[ViewState["lstAboutUSGrid"].ToString()];

        }
        set
        {
            if (ViewState["lstAboutUSGrid"] == null)
            {
                ViewState["lstAboutUSGrid"] = System.Guid.NewGuid().ToString();
            }

            Session[ViewState["lstAboutUSGrid"].ToString()] = value;
        }   
   
   

    }




    long mAboutUSID
    {
        get
        {
            if (ViewState["mAboutUSID"] == null)
            {
                ViewState["mAboutUSID"] = System.Guid.NewGuid().ToString();
            }


            if (Session[ViewState["mAboutUSID"].ToString()] == null)
            {
                long obj = -1;
                Session[ViewState["mAboutUSID"].ToString()] = obj;

            }

            return (long)Session[ViewState["mAboutUSID"].ToString()];

        }
        set
        {
            if (ViewState["mAboutUSID"] == null)
            {
                ViewState["mAboutUSID"] = System.Guid.NewGuid().ToString();
            }

            Session[ViewState["mAboutUSID"].ToString()] = value;
        }
    }


    AboutUS mObjAboutUS
    {
        get
        {
            if (ViewState["mObjAboutUS"] == null)
            {
                ViewState["mObjAboutUS"] = System.Guid.NewGuid().ToString();
            }


            if (Session[ViewState["mObjAboutUS"].ToString()] == null)
            {
                Session[ViewState["mObjAboutUS"].ToString()] = new AboutUS();

            }

            return (AboutUS)Session[ViewState["mObjAboutUS"].ToString()];

        }
        set
        {
            if (ViewState["mObjAboutUS"] == null)
            {
                ViewState["mObjAboutUS"] = System.Guid.NewGuid().ToString();
            }

            Session[ViewState["mObjAboutUS"].ToString()] = value;
        }
    }






    protected void Page_Load(object sender, EventArgs e)
    {



        Persons objPersons = GlobalVars.getCurrentPerson();
        checkSecurityAndCustomizePage(objPersons);
       
        if (!Page.IsPostBack)
        {
            
            bindGrid("");
            editLoad();
        }

        

    }



    /// <summary>
    /// this function will bind the grid 
    /// </summary>
    protected void bindGrid(string strIn)
    {

        List<AboutUS> lstAboutUS = search(strIn);



        lstAboutUSGrid = lstAboutUS;

        grdUsers.DataSource = lstAboutUS;
        grdUsers.DataBind();
    }



    /// <summary>
    /// it will check the security and customize the page
    /// </summary>
    /// <param name="currentPerson">the person to set security to his previleges</param>
    protected void checkSecurityAndCustomizePage(Persons currentPerson)
    {
        //Security Check

        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.About_us_page))
            Response.Redirect("LoginEN.aspx");


        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Editing_about_us_page))
        {
            btnSave.Enabled = false;
            btnDelete.Enabled = false;
        }
       




    }
    
    protected void btnSearch_Click(object sender, EventArgs e)
    {

        bindGrid(txtSearch.Text);
       
    }

    /// <summary>
    /// this function will search in About us and return the values
    /// </summary>
    /// <param name="txtText">the text to search for</param>
    /// <returns>a list of About us objects matched</returns>
    protected List<AboutUS> search(string txtText)
    {
        AboutUSDB objAboutUSDB = new AboutUSDB(ConnectionStrings.LMS);
        List<AboutUS> lstRet = new List<AboutUS>();
        if (txtText.Trim() == "")
        {
            List<AboutUS> lstNew = objAboutUSDB.Select(" top 100 ", " order by [AboutEN] ");
            //lstNew = new List<AboutUS>(from objFol in lstNew where objFol.StatusID != 2 select objFol);
            //no status
            return lstNew;
        }

        string[] strValues = null;
        if (txtText.Trim()[0] == '\"' && txtText.Trim()[txtText.Trim().Length - 1] == '\"')
        {
            try
            {
                strValues = new string[] { txtText.Trim().Substring(1, txtText.Trim().Length - 2) };
                if (strValues[0] == "")
                    strValues = new string[0];
            }
            catch
            {
                strValues = new string[0];
            }
        }
        else
        {
            strValues = txtText.Trim().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
        }

        foreach (string strValue in strValues)
        {
            AboutUS objAboutUS = new AboutUS();
            objAboutUS.AboutAR = objAboutUS.AboutEN = "%" + strValue + "%";
            List<AboutUS> lstAboutUS = objAboutUSDB.SearchOrLike(objAboutUS);
            //lstAboutUS = new List<AboutUS>(from objFol in lstAboutUS where objFol.StatusID != 2 select objFol);
            //no status
            foreach (AboutUS objFol in lstAboutUS)
            {
                if ((from objIn in lstRet where objIn.ID == objFol.ID select objIn).ToList().Count == 0)
                {
                    lstRet.Add(objFol);
                }
            }

        }


        return lstRet;



    }





    protected void grdUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        Persons objCurrentPersons = GlobalVars.getCurrentPerson();
        checkSecurityAndCustomizePage(objCurrentPersons);

        //security check

        if (!objCurrentPersons.lstAllRulesIDs.Contains((long)RulesEnum.Editing_about_us_page))
        {
            lblError.Text = "Security error , you can view only";
            lblResult.Text = "";

            return;
        }


        long AboutUSID = long.Parse(grdUsers.DataKeys[e.RowIndex]["ID"].ToString());



        AboutUSDB objAboutUSDB = new AboutUSDB(ConnectionStrings.LMS);
        List<AboutUS> lstAboutUS = objAboutUSDB.getAboutUSByID(AboutUSID);




        if (lstAboutUS.Count == 0)
        {
            lblError.Text = "No About us data to delete";
            lblResult.Text = "";
            return;
        }





       


        objAboutUSDB.Delete(lstAboutUS[0]);



        lblResult.Text = "Deleted ok.";



        bindGrid(txtSearch.Text);



    }

    
    protected void btnNew_Click(object sender, EventArgs e)
    {
        clearAllControls();
    }


    protected void grdUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdUsers.DataSource = lstAboutUSGrid;
        grdUsers.PageIndex = e.NewPageIndex;

        grdUsers.DataBind(); 
    }


    /// <summary>
    /// this function will check mAboutUSID to detect update or insert
    /// </summary>
    protected void checkID()
    {
        if (mAboutUSID != -1)
        {
            try
            {

                AboutUSDB objAboutUSDB = new AboutUSDB(ConnectionStrings.LMS);
                List<AboutUS> lstAboutUS = objAboutUSDB.getAboutUSByID(mAboutUSID);
                if (lstAboutUS.Count > 0)
                {


                    mObjAboutUS = lstAboutUS[0];
                    btnDelete.Visible = true;

                }
                else
                {
                    mAboutUSID = ((long)-1);
                    
                }


            }
            catch
            {
                mAboutUSID = ((long)-1);
            }


            
        }
        else
        {
            btnDelete.Visible = false;

        }
    }



    /// <summary>
    /// this will bind the About us data to update or delete
    /// </summary>
    protected void BindAllControls()
    {
        if (mAboutUSID != -1)
        {


            txtAboutAR.Text = mObjAboutUS.AboutAR;
            txtAboutEN.Text = mObjAboutUS.AboutEN;
            

        }
    }



    /// <summary>
    /// this function will read all controls values
    /// </summary>
    protected void ReadAllControls()
    {

        mObjAboutUS.AboutAR = txtAboutAR.Text;
        mObjAboutUS.AboutEN = txtAboutEN.Text;
        


    }



    /// <summary>
    /// this function for clearing controls
    /// </summary>
    protected void clearAllControls()
    {

        txtAboutAR.Text = "";
        txtAboutEN.Text = "";

        mObjAboutUS = new AboutUS();

        mAboutUSID = -1;

    }



    /// <summary>
    /// this function for loading the editing behavior
    /// </summary>
    protected void editLoad()
    {


        Persons currentPerson = GlobalVars.getCurrentPerson();




       

            checkSecurityAndCustomizePage(currentPerson);

            checkID();


            BindAllControls();




        
    }



    /// <summary>
    /// this function will Save the data
    /// </summary>
    protected void Save()
    {


        if (checkForRepeats())
        {
            AboutUSDB objAboutUSDB = new AboutUSDB(ConnectionStrings.LMS);
            if (mObjAboutUS.ID == -1)
            {
                objAboutUSDB.Insert(mObjAboutUS);
                clearAllControls();
                bindGrid(txtSearch.Text);
                lblResult.Text = "Data saved ok.";
            }
            else
            {
                objAboutUSDB.Update(mObjAboutUS);
                bindGrid(txtSearch.Text);
                BindAllControls();
                lblResult.Text = "Data saved ok.";

            }



        }


    }


    /// <summary>
    /// this function will check if the data exist with the same Name or not
    /// </summary>
    /// <returns>true if ok , false for error (data  exist before)</returns>
    protected bool checkForRepeats()
    {

        return true;
        //AboutUSDB objAboutUSDB = new AboutUSDB(ConnectionStrings.LMS);



        //List<AboutUS> lstOldAboutUS = objAboutUSDB.getAboutUSByAboutUSNameAR(mObjAboutUS.AboutUSNameAR);

        //List<AboutUS> lstExist = new List<AboutUS>(from objOrg in lstOldAboutUS where (objOrg.AboutUSNameAR == mObjAboutUS.AboutUSNameAR && objOrg.ID != mObjAboutUS.ID) select objOrg);

        //if (lstExist.Count > 0)
        //{

        //    errorExist();
        //    return false;
        //}


        // lstOldAboutUS = objAboutUSDB.getAboutUSByAboutUSNameEN(mObjAboutUS.AboutUSNameEN);

        // lstExist = new List<AboutUS>(from objOrg in lstOldAboutUS where (objOrg.AboutUSNameEN == mObjAboutUS.AboutUSNameEN && objOrg.ID != mObjAboutUS.ID) select objOrg);

        //if (lstExist.Count > 0)
        //{

        //    errorExist();
        //    return false;
        //}





        //return true;

    }




    /// <summary>
    /// this function will show a message with the error for existense
    /// </summary>
    private void errorExist()
    {

        lblError.Text = "Error , the data  is already found !";

    }



    protected void btnSave_Click(object sender, EventArgs e)
    {

        //security check
        Persons currentPerson = GlobalVars.getCurrentPerson();
        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Editing_about_us_page))
            return;


        if (Page.IsValid)
        {



            ReadAllControls();






            Save();


        }
    }




    protected void btnDelete_Click(object sender, EventArgs e)
    {

        Persons objCurrentPersons = GlobalVars.getCurrentPerson();
        checkSecurityAndCustomizePage(objCurrentPersons);

        //security check

        if (!objCurrentPersons.lstAllRulesIDs.Contains((long)RulesEnum.Editing_about_us_page))
        {
            lblError.Text = "Security error , you can view only";
            lblResult.Text = "";

            return;
        }


        long AboutUSID = mObjAboutUS.ID;



        AboutUSDB objAboutUSDB = new AboutUSDB(ConnectionStrings.LMS);
        List<AboutUS> lstAboutUS = objAboutUSDB.getAboutUSByID(AboutUSID);




        if (lstAboutUS.Count == 0)
        {
            lblError.Text = "No About us data to delete";
            lblResult.Text = "";
            return;
        }








        objAboutUSDB.Delete(lstAboutUS[0]);



        lblResult.Text = "Deleted ok.";



        bindGrid(txtSearch.Text);






    }




    protected void grdUsers_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Persons objCurrentPersons = GlobalVars.getCurrentPerson();
        checkSecurityAndCustomizePage(objCurrentPersons);

        

        long AboutUSID = long.Parse(grdUsers.DataKeys[e.NewEditIndex]["ID"].ToString());

        mAboutUSID = AboutUSID;

        editLoad();

        e.Cancel = true;

    }
}
